class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());
        int l1 = nums1.size(), l2 = nums2.size();
        vector<int> ret;
        int n1 = 0, n2 = 0;
        while (n1 < l1 && n2 < l2)
        {
            if (nums1[n1] > nums2[n2])
                n2++;
            else if (nums1[n1] < nums2[n2])
                n1++;
            else
            {
                ret.push_back(nums1[n1]), n1++, n2++;
                while (n1 < l1 && nums1[n1] == nums1[n1 - 1]) n1++;
                while (n2 < l2 && nums2[n2] == nums2[n2 - 1]) n2++;
            }
        }
        return ret;
    }
};